package com.sgb.kjwl.view.widget.pick;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.LayoutPickPopwindowViewBinding;

/**
 * 招采模块-PopWindow下拉新样式
 */
public class PickPopWindowView extends LinearLayout {

    private  int textcolor ;
    private Context mContext;
    private LayoutPickPopwindowViewBinding binding;
    private String textContextDefault;
    private Drawable lineBgDefault;
    private Drawable imageBgDefault;

    public PickPopWindowView(Context context) {
        this(context, null);
    }

    public PickPopWindowView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        binding = DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.layout_pick_popwindow_view, this, true);
        if (attrs != null) {
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.PickPopWindowView);
            textContextDefault = typedArray.getString(R.styleable.PickPopWindowView_text_context);
            int color = context.getResources().getColor(R.color.color_tab_one);
            textcolor = typedArray.getColor(R.styleable.PickPopWindowView_text_color,color);
            lineBgDefault = typedArray.getDrawable(R.styleable.PickPopWindowView_line_bg);
            imageBgDefault = typedArray.getDrawable(R.styleable.PickPopWindowView_image_bg);
            typedArray.recycle();
        }
        initView();
    }

    private void initView(){
        binding.popTv.setText(textContextDefault);
        binding.popTv.setTextColor(textcolor);
        binding.popLayout.setBackground(lineBgDefault);
        binding.popImage.setBackground(imageBgDefault);
    }

    /**
     * 选中更改UI
     */
    public void setChooseData(int lineBg, int imageBg, int textColor,boolean isShow,String textContext) {
        if(TextUtils.isEmpty(textContext)){
            binding.popTv.setText(textContextDefault);
        }else{
            binding.popTv.setText(textContext);
        }
        binding.popTv.setTextColor(mContext.getResources().getColor(textColor));
        binding.popLayout.setBackground(mContext.getResources().getDrawable(lineBg));
        binding.popImage.setBackground(mContext.getResources().getDrawable(imageBg));
        if(isShow){
            binding.popLine.setMinimumWidth(binding.popLayout.getMeasuredWidth());
            binding.popLine.setVisibility(View.VISIBLE);
        }else{
            binding.popLine.setVisibility(View.GONE);
        }
    }

}

